Godot 4.x: Callable の call と call_deferred の違い
結論
call は即時呼び出し、call_deferred は次フレーム以降のアイドル時呼び出し
現在フレームで必須な処理じゃなければ、call_deferred 使ったほうが安全
void call_deferred ( ... ) vararg const
Calls the method represented by this Callable in deferred mode, i.e. during the idle frame. Arguments can be passed and should match the method's signature.
deferred mode で呼び出すモード
deferred mode は呼び出しを一旦待機して、次フレーム以降のアイドル時に実行する
A simple answer is that call_deffered() calls a function in the main thread when it is not doing anything / idle. There is no specific delay length, it just runs when there is nothing on the main thread.
一度に大量の処理を行う場合は、call_deferred を使ったほうがパフォーマンス上の問題になりにくいことが分かる
However, changing this line:
emit_signal(MESSAGE_RECEIVED, msg)
to this:
call_deferred("emit_signal", MESSAGE_RECEIVED, msg)
gave me the expected result, and was able to render all 500 scenes within 100ms.
試しに以下のコードを実行すると
code:gd
func _ready() -> void:
print("_ready!")
exec_callable(func(x:String):print("hello: " + x))
func exec_callable(f: Callable) -> void:
f.call_deferred("call_deferred")
f.call("call")
関数の呼び出し順番的には、call_deferred の方が先だが、出力順は
code:txt
_ready!
hello: call
hello: call_deferred
call の方が先になる